package cn.kkcxy.blog.system.service.impl.sys;

import cn.kkcxy.blog.common.constant.ExceptionMessageConstant;
import cn.kkcxy.blog.core.entity.sys.SysRole;
import cn.kkcxy.blog.core.exception.auth.SystemRoleException;
import cn.kkcxy.blog.core.vo.admin.system.SimpleRoleInfoVO;
import cn.kkcxy.blog.system.mapper.sys.SysRoleMapper;
import cn.kkcxy.blog.system.service.sys.SysRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author kk
* @description 针对表【sys_role(角色)】的数据库操作Service实现
* @createDate 2024-09-11 00:50:27
*/
@Slf4j
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService{

    /**
     * 系统角色mapper
     */
    @Resource
    private SysRoleMapper sysRoleMapper;

    /**
     * @description: 根据角色编码获得角色Id
     * @author: kkcxy
     * @date: 2024/9/12 1:32
     * @param: [roleNormalUser]
     * @return: cn.kkcxy.blog.core.entity.sys.SysRole
     **/
    @Override
    public SysRole getRoleCodeByRoleId(String roleNormalUser) {
        // 判断传递的角色编码是否为空
        if (StringUtils.isEmpty(roleNormalUser)) {
            throw new SystemRoleException(ExceptionMessageConstant.SYSTEM_ROLE_CODE_NOT_FOUND);
        }
        // 封装查询角色条件
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRole::getRoleCode, roleNormalUser);
        // 查询角色信息
        return sysRoleMapper.selectOne(queryWrapper);
    }

    /**
     * @description: 获取角色列表
     * @author: kkcxy
     * @date: 2024/10/7 21:29
     * @param: [userId]
     * @return: java.util.List<java.lang.String>
     **/
    @Override
    public List<String> getRoleList(Long userId) {
        log.info("获取角色列表 id: {}", userId);
        return sysRoleMapper.getRoleList(userId);
    }

    /**
     * @description: 得到管理员角色名称
     * @author: kkcxy
     * @date: 2024/10/29 1:18
     * @param: [adminId]
     * @return: java.lang.String
     **/
    @Override
    public String getRoleNameByAdministratorId(Long adminId) {
        return sysRoleMapper.getRoleNameByAdministratorId(adminId);
    }

    /**
     * 得到用户搜索条件的角色列表
     */
    @Override
    public List<SimpleRoleInfoVO> getUserSearchConditionsRoleList() {
        return sysRoleMapper.getUserSearchConditionsRoleList();
    }
}




